<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container">
    <nav class="tab">
      <a @click="hasAccount=true" :class="{active:hasAccount}" href="javascript:;">
        <i class="iconfont icon-bind" />
 <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a @click="hasAccount=false" :class="{active:!hasAccount}" href="javascript:;">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind   :unionId="unionId" />
      <!-- 已有小兔鲜账号，请绑定手机 -->
      <!-- 后边再来放组件 -->
    </div>
    <div class="tab-content" v-else>
       <CallbackPatch :unionId="unionId" />
      <!-- 请完善资料 -->
      <!-- 后边再来放组件 -->
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import LoginHeader from './components/login-header.vue'
import LoginFooter from './components/login-footer.vue'
import QC from 'qc'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import CallbackBind from './components/callback-bing.vue'
import CallbackPatch from './components/callback-patch.vue'
import { ref } from 'vue'
import { userQQLogin } from '@/api/user'
import Message from '@/components/message'
export default {
  name: 'PageCallback',
  components: { CallbackBind, CallbackPatch, LoginHeader, LoginFooter },
  setup () {
    const store = useStore()
    const router = useRouter()
    const unionId = ref('')
    QC.Login.check() && QC.Login.getMe(openId => {
      unionId.value = openId
      // 请求小兔仙，做qq登录
      console.log('openId', openId)

      userQQLogin(openId).then(async data => {
        // 直接进入页面
        // 路由跳转
        // 绑定成功
        console.log(data)
        // 实现和之前登录一样的逻辑
        // 存储用户信息
        store.commit('user/setUser', data.result)
        await store.dispatch('cart/mergeLocalCart')
        // 路由跳转
        router.push('/')
        Message({ type: 'success', text: '登录成功' })
      }).catch(e => {
        // 停留在本页面
      })
    })
    const hasAccount = ref(true)
    return { hasAccount, unionId }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
